SUBROUTINE G_GRID(i)
!
! Definition von G:  Gittergleichung
!

      use config,  only : np
      use primvar, only : G, X, MR, Gnorm
      use physco,  only : pi, offset
      use geomvar, only : S_flux
      use gridvar, only : Res, n_tmp, lagr_upto
      use RBvar,   only : R_in, R_out


      implicit none

      integer, intent(in) :: i

      logical, save :: init = .true.
      if (init) then
         write(66,"(a)") "G_grid.f90:   Innenrand: R = R_in ;  i = 4: Lagrange ;  Aussenrand: R = R_out"
         init = .false.
      end if


!-----------------------------------------------------------------------
!    Randbedingungen
!-----------------------------------------------------------------------

! innere & aeussere Randbedingungen
RAND: if( i == 1 .or. i == 2 .or. i == 3 ) then            ! innere Pseudozellen
         G(MR,i)     = X(MR,i) - R_in
         Gnorm(MR,i) = max( X(MR,i), offset )
      else if( i == np .or. i == np-1 ) then               ! aeussere Pseudozellen
         G(MR,i)     = X(MR,i) - X(MR,i-1)
         Gnorm(MR,i) = X(MR,i)
      else if ( 3 < i .and. i <= lagr_upto ) then          ! moeglich: lagr_upto = 4 ... np-3

         ! Lagrange Gittergleichung:
         !   - zumindest fuer einen Gitterpunkt (4)
         !   - maximal ueber den ganzen Bereich bis zum Aussenrand (np-3)
         G(MR,i)     = S_flux(i)
         Gnorm(MR,i) = pi * X(MR,i)**2

      else if( i == np-2 ) then                            ! Gleichung fuer den Aussenradius
         G(MR,i)     = X(MR,i) - R_out                     !    fixer Aussenradius
         Gnorm(MR,i) = max( X(MR,i), offset )
      else                                                 ! Gittergleichung sonst

!-----------------------------------------------------------------------
!    Gittergleichung
!-----------------------------------------------------------------------

         G(MR,i) = n_tmp(i) / Res(i) - n_tmp(i-1) / Res(i-1)

         Gnorm(MR,i) = max( n_tmp(i) / Res(i), n_tmp(i-1) / Res(i-1) )

      end if RAND


END SUBROUTINE G_GRID

